function getContextPath() {
  let strPath = window.document.location.pathname;
  return strPath.substring(0, strPath.substr(1).indexOf('/') + 1);
}

function getTime() {
  let time = new Date();
  let hour = time.getHours() < 10 ? '0' + time.getHours() : time.getHours(),
    minutes = time.getMinutes() < 10 ? '0' + time.getMinutes() : time.getMinutes(),
    seconds = time.getSeconds() < 10 ? '0' + time.getSeconds() : time.getSeconds();
  return hour + ':' + minutes + ':' + seconds;
}

function getDate() {
  let time = new Date();
  let week = {
    '0': '日',
    '1': '一',
    '2': '二',
    '3': '三',
    '4': '四',
    '5': '五',
    '6': '六'
  };
  let year = time.getFullYear(),
    month = time.getMonth() + 1,
    date = time.getDate(),
    day = time.getDay(),
    hour = time.getHours(),
    ap = hour > 12 ? 'PM' : 'AM';
  return year + '/' + month + '/' + date + ' ' + ' ' + ap + ' ' + ' ' + '周' + week[day];
}

function getFullDate() {
  let time = new Date();
  let week = {
    '0': '日',
    '1': '一',
    '2': '二',
    '3': '三',
    '4': '四',
    '5': '五',
    '6': '六'
  };
  let year = time.getFullYear(),
    month = time.getMonth() + 1,
    date = time.getDate(),
    day = time.getDay(),
    hour = time.getHours() < 10 ? '0' + time.getHours() : time.getHours(),
    minutes = time.getMinutes() < 10 ? '0' + time.getMinutes() : time.getMinutes(),
    seconds = time.getSeconds() < 10 ? '0' + time.getSeconds() : time.getSeconds();
  return year + '年' + month + '月' + date + '日' + ' ' + hour + ':' + minutes + ':' + seconds + ' ' + '周' + week[day];
}

/**
 * 通过天气网获取天气信息
 * @param city  目标城市的天气
 * @param cityID  DOM元素必须设置ID
 * @param picID  设置天气图标必须设置ID
 * @param num  选择图标大小 1:50px 2:100px
 * */
function getWeather(city, cityID, picID, num) {
  let result;
  let result1;
  let high;
  let low;
  let type;
  let fengxiang;
  let xmlhttp;
  if (window.XMLHttpRequest) {
    //  IE7+, Firefox, Chrome, Opera, Safari 浏览器执行代码
    xmlhttp = new XMLHttpRequest();
  } else {
    // IE6, IE5 浏览器执行代码
    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
  }
  //建立连接
  xmlhttp.open("GET", "http://wthrcdn.etouch.cn/weather_mini?city=" + city, true);
  //发送请求
  xmlhttp.send();
  //响应
  xmlhttp.onreadystatechange = function () {
    if (xmlhttp.readyState === 4 && xmlhttp.status === 200) {
      let obj = JSON.parse(xmlhttp.responseText);     //  将json数据转换成对象
      let smg = obj.data.forecast;
      high = smg[0].high;
      low = smg[0].low;
      type = smg[0].type;
      fengxiang = smg[0].fengxiang;
      // let a = high.split(" ")[1];
      // result = type + " " + high + " " + low + " " + fengxiang;
      result = low.split(" ")[1] + " ~ " + high.split(" ")[1] + " ";
      result1 = fengxiang + " " + type;
      document.getElementById(cityID).innerText = result;
      document.getElementById(cityID).nextElementSibling.innerText = result1;
      // console.log(smg[3].date);    //  大后天天气
      //  添加天气图例
      if (num === 1) {
        switch (type) {
          case "暴雪":
            document.getElementById(picID).style.backgroundImage = "url('images/weather(50px)/xue.png')";
            break;
          case "暴雨":
            document.getElementById(picID).style.backgroundImage = "url('images/weather(50px)/yu.png')";
            break;
          case "大暴雨":
            document.getElementById(picID).style.backgroundImage = "url('images/weather(50px)/xue.png')";
            break;
          case "大雪":
            document.getElementById(picID).style.backgroundImage = "url('images/weather(50px)/xue.png')";
            break;
          case "大雨":
            document.getElementById(picID).style.backgroundImage = "url('images/weather(50px)/yu.png')";
            break;
          case "冻雨":
            document.getElementById(picID).style.backgroundImage = "url('images/weather(50px)/yu.png')";
            break;
          case "多云":
            document.getElementById(picID).style.backgroundImage = "url('images/weather(50px)/duoyun.png')";
            break;
          case "浮尘":
            document.getElementById(picID).style.backgroundImage = "url('images/weather(50px)/duoyun.png')";
            break;
          case "雷阵雨":
            document.getElementById(picID).style.backgroundImage = "url('images/weather(50px)/yu.png')";
            break;
          case "雷阵雨伴有冰雹":
            document.getElementById(picID).style.backgroundImage = "url('images/weather(50px)/leizhenyu.png')";
            break;
          case "霾":
            document.getElementById(picID).style.backgroundImage = "url('images/weather(50px)/duoyun.png')";
            break;
          case "强沙尘暴":
            document.getElementById(picID).style.backgroundImage = "url('images/weather(50px)/duoyun.png')";
            break;
          case "晴":
            document.getElementById(picID).style.backgroundImage = "url('images/weather(50px)/qing.png')";
            break;
          case "沙尘暴":
            document.getElementById(picID).style.backgroundImage = "url('images/weather(50px)/duoyun.png')";
            break;
          case "特大暴雨":
            document.getElementById(picID).style.backgroundImage = "url('images/weather(50px)/yu.png')";
            break;
          case "雾":
            document.getElementById(picID).style.backgroundImage = "url('images/weather(50px)/duoyun.png')";
            break;
          case "小雪":
            document.getElementById(picID).style.backgroundImage = "url('images/weather(50px)/xue.png')";
            break;
          case "小雨":
            document.getElementById(picID).style.backgroundImage = "url('images/weather(50px)/yu.png')";
            break;
          case "扬沙":
            document.getElementById(picID).style.backgroundImage = "url('images/weather(50px)/duoyun.png')";
            break;
          case "阴":
            document.getElementById(picID).style.backgroundImage = "url('images/weather(50px)/yin.png')";
            break;
          case "雨夹雪":
            document.getElementById(picID).style.backgroundImage = "url('images/weather(50px)/xue.png')";
            break;
          case "阵雪":
            document.getElementById(picID).style.backgroundImage = "url('images/weather(50px)/xue.png')";
            break;
          case "阵雨":
            document.getElementById(picID).style.backgroundImage = "url('images/weather(50px)/yu.png')";
            break;
          case "中雪":
            document.getElementById(picID).style.backgroundImage = "url('images/weather(50px)/xue.png')";
            break;
          case "中雨":
            document.getElementById(picID).style.backgroundImage = "url('images/weather(50px)/yu.png')";
            break;
          default:
            break;
        }
      } else if (num === 2) {
        switch (type) {
          case "暴雪":
            document.getElementById(picID).style.backgroundImage = "url('images/weather(100px)/xue.png')";
            break;
          case "暴雨":
            document.getElementById(picID).style.backgroundImage = "url('images/weather(100px)/yu.png')";
            break;
          case "大暴雨":
            document.getElementById(picID).style.backgroundImage = "url('images/weather(100px)/xue.png')";
            break;
          case "大雪":
            document.getElementById(picID).style.backgroundImage = "url('images/weather(100px)/xue.png')";
            break;
          case "大雨":
            document.getElementById(picID).style.backgroundImage = "url('images/weather(100px)/yu.png')";
            break;
          case "冻雨":
            document.getElementById(picID).style.backgroundImage = "url('images/weather(100px)/yu.png')";
            break;
          case "多云":
            document.getElementById(picID).style.backgroundImage = "url('images/weather(100px)/duoyun.png')";
            break;
          case "浮尘":
            document.getElementById(picID).style.backgroundImage = "url('images/weather(100px)/duoyun.png')";
            break;
          case "雷阵雨":
            document.getElementById(picID).style.backgroundImage = "url('images/weather(100px)/yu.png')";
            break;
          case "雷阵雨伴有冰雹":
            document.getElementById(picID).style.backgroundImage = "url('images/weather(100px)/leizhenyu.png')";
            break;
          case "霾":
            document.getElementById(picID).style.backgroundImage = "url('images/weather(100px)/duoyun.png')";
            break;
          case "强沙尘暴":
            document.getElementById(picID).style.backgroundImage = "url('images/weather(100px)/duoyun.png')";
            break;
          case "晴":
            document.getElementById(picID).style.backgroundImage = "url('images/weather(100px)/qing.png')";
            break;
          case "沙尘暴":
            document.getElementById(picID).style.backgroundImage = "url('images/weather(100px)/duoyun.png')";
            break;
          case "特大暴雨":
            document.getElementById(picID).style.backgroundImage = "url('images/weather(100px)/yu.png')";
            break;
          case "雾":
            document.getElementById(picID).style.backgroundImage = "url('images/weather(100px)/duoyun.png')";
            break;
          case "小雪":
            document.getElementById(picID).style.backgroundImage = "url('images/weather(100px)/xue.png')";
            break;
          case "小雨":
            document.getElementById(picID).style.backgroundImage = "url('images/weather(100px)/yu.png')";
            break;
          case "扬沙":
            document.getElementById(picID).style.backgroundImage = "url('images/weather(100px)/duoyun.png')";
            break;
          case "阴":
            document.getElementById(picID).style.backgroundImage = "url('images/weather(100px)/yin.png')";
            break;
          case "雨夹雪":
            document.getElementById(picID).style.backgroundImage = "url('images/weather(100px)/xue.png')";
            break;
          case "阵雪":
            document.getElementById(picID).style.backgroundImage = "url('images/weather(100px)/xue.png')";
            break;
          case "阵雨":
            document.getElementById(picID).style.backgroundImage = "url('images/weather(100px)/yu.png')";
            break;
          case "中雪":
            document.getElementById(picID).style.backgroundImage = "url('images/weather(100px)/xue.png')";
            break;
          case "中雨":
            document.getElementById(picID).style.backgroundImage = "url('images/weather(100px)/yu.png')";
            break;
          default:
            break;
        }
      }
    }
  };
}

function getW(id) {
  document.getElementById(id).style.background = "url('images/weather(50px)/yu.png')";
}

/**
 *
 * 修改站点信息
 *
 */
function getMessage(object) {
  document.getElementById("provinceName").innerText = object.provinceName;
  document.getElementById("siteName").innerText = object.siteName;
  document.getElementById("siteNumber").innerText = object.siteNumber;
  document.getElementById("lidarType").innerText = object.lidarType;
  document.getElementById("altitude").innerText = object.altitude;
  document.getElementById("siteLon").innerText = object.siteLon;
  document.getElementById("siteLat").innerText = object.siteLat;
}
